package dataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CreateDBTables {

	private static final int MAX_TABLES = 6;
	private static final String USER_RACE = "CREATE TABLE user_race ("
			+ "race_id NUMBER NOT NULL, " + "race_name VARCHAR2(10), "
			+ "CONSTRAINT race_pk PRIMARY KEY(race_id)) ";
	private static final String USER_CITY = "CREATE TABLE user_city ("
			+ "city_id NUMBER NOT NULL, " + "city_name VARCHAR2(15), "
			+ "CONSTRAINT city_pk PRIMARY KEY(city_id)) ";
	private static final String USER_SEQUENCE = "CREATE SEQUENCE user_seq START WITH 1 ";
	private static final String BUILDINGS = "CREATE TABLE buildings (barack_id NUMBER, "
			+ "goldmine_id NUMBER, " + "foodstore_id NUMBER) ";
	private static final String HP_AD_LEVELS = "CREATE TABLE hp_ad_levels("
			+ "username VARCHAR2(20), barbarianHPLevel NUMBER, "
			+ "barbarianADLevel NUMBER, archerHPLEvel NUMBER, "
			+ "archers NUMBER, barbarians NUMBER, wizzards NUMBER, "
			+ "archerADLevel NUMBER, wizzardADLevel NUMBER, "
			+ "wizzardHPLevel NUMBER) ";
	private static final String USERS = "CREATE TABLE users (user_id NUMBER NOT NULL, "
			+ "username VARCHAR2(15) UNIQUE NOT NULL, "
			+ "password VARCHAR2(15) NOT NULL, "
			+ "age NUMBER NOT NULL, "
			+ "race_id NUMBER NOT NULL, "
			+ "city_id NUMBER NOT NULL, "
			+ "gold NUMBER NOT NULL, "
			+ "food NUMBER, "
			+ "army_capacity NUMBER, "
			+ "army NUMBER, "
			+ "food_production NUMBER, "
			+ "gold_production NUMBER, "
			+ "CONSTRAINT user_pk PRIMARY KEY(user_id), "
			+ "CONSTRAINT race_fk FOREIGN KEY(race_id) REFERENCES user_race(race_id), "
			+ "CONSTRAINT city_fk FOREIGN KEY(city_id) REFERENCES user_city(city_id)) ";
	private static String table;

	public static void createAllTables(Connection dbCon) throws SQLException {
		for (int i = 0; i < MAX_TABLES; i++) {
			checkTableIndex(i);
			PreparedStatement statement = dbCon.prepareStatement(table);
			statement.executeUpdate();
		}
	}

	private static void checkTableIndex(int i) {
		if (i == 0)
			table = USER_RACE;
		if (i == 1)
			table = USER_CITY;
		if (i == 2)
			table = USER_SEQUENCE;
		if (i == 3)
			table = BUILDINGS;
		if (i == 4)
			table = HP_AD_LEVELS;
		if (i == 5)
			table = USERS;
	}
}
